#!/usr/bin/env python
# -*- coding:utf-8 -*-

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Copyright (C) 2012 Yair Eshel Cahansky
# Authors: 
#    Yair Eshel Cahansky (2012) <guruyaya@gmail.com>
#    Yaacov Zamir (2012) <kobi.zamir@gmail.com>

from gluon.serializers import json

def get_translations():
    ''' gets translatoins into a JS files
    '''
    try:
       langfile = open(T.language_file, 'r')
       trans_strings = eval(langfile.read())
    except:
        trans_strings = dict()
       # javascript T function
    
    add_expires_header(response)
    
    output = '''
T = function(trans_string){
    if (typeof T.strings[ trans_string ] == 'undefined')
         return trans_string;
    return T.strings[  trans_string ];
}
'''
    output += 'T.strings = %s' % json(trans_strings)
       
    return XML(output)

def add_expires_header(response):
    ''' add expire to give better caching to JS files
    '''
    expires = time.time() + 1209600 # = 14 * 24 * 3600 = 14 days from now
    response.headers['Expires'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(expires))
    